<?php
    /**
    * Term类，用于以数组的方式处理栏目
    */
    class Term
    {
        private $termList=null;
        private $address=null;
        private $termId=null;
        private $getDate=null;
        private $termInfo

        function __construct()
        {
            $this->termId=I('get.term_id')?I('get.term_id'):1;
        }

        //getList方法：获得一个经过处理的栏目列表
        private function getList()
        {
            if($this->termList!=null){
                return $this->termList;
            }
            $termList  =  M('term')->where(array('status'=>'1'))->order('listorder')->select();
            foreach($termList as $termVo){
                $newTermList[$termVo['id']] = $termVo;
            }

            foreach($newTermList as $key => $termVo){
                if($term_parent_id = $termVo['parent_id']){
                    if (isset($newTermList[$term_parent_id])) {
                        $newTermList[$term_parent_id]["has_child"]=true;
                    }
                }
            }
            $this->termList=$newTermList;
            return $this->termList;
        }

        //getChild方法：获得某栏目ID下的子栏目列表;
        public function getChild($termId=null)
        {
            if($termId===null)$termId=$this->termId;
            $termList=$this->getList();
            foreach($termList as $key => $termVo){
                if($termVo['parent_id']==$termId){
                    $termChildList[$key] = $termVo;
                }
            }
            return $termChildList;
        }

        //whereAmI方法：查询当前栏目所处的位置，返回一个数组，从头到尾依次是从顶级栏目到本栏目所经过的栏目信息，键名为栏目ID
        public function whereAmI()
        {
            if($this->address!=null){
                return $this->address;
            }
            $termId=$this->termId;
            $termList=$this->getList();
            $address = array();
            while (!empty($termList[$termId]['id'])){
                $address[$termId] = $termList[$termId];
                $termId = $termList[$termId]['parent_id'];
            }
            $newAddress = array_reverse($address,true);
            $this->address=$newAddress;
            return $newAddress;
        }

        //link方法：根据id生成一个链接地址，参数依次为ID、传参，是否继承参数
        public function link($termId=null, $param=array(), $extend=false)
        {
            $get=$this->hasGet();
            $termId=$termId?$termId:$this->termId;
            $termList=$this->getList();
            if($extend&&$get) $param = array_merge($get,$param);
            $param['term_id']=$termId;
            $url = U('Index/'.$termList[$termId]['tpl_name'], $param);
            return $url;
        }

        //hasGet方法：获取Get数据
        private function hasGet()
        {
            if($this->getDate!==null){
                return $this->getDate;
            }
            $get=I('get.');
            unset($get['_URL_']);
            unset($get['term_id']);
            if(!count($get))$get=false;
            $this->getDate=$get;
            return $get;
        }

        //extendField方法：继承字段，返回本栏目的一个字段，如果没有该字段，则查看父栏目，直到找到该字段或到达顶级栏目
        public function extendField($field)
        {
            $address=$this->whereAmI();
            $fieldValue=null;
            $TermId=$this->termId;
            while(!$fieldValue && !$address[$TermId]){
                $fieldValue = $address[$TermId][$field];
                $TermId = $address[$TermId]['parent_id'];
            }
            return $fieldValue;
        }

        //getInfo方法：获取当前栏目的详细信息
        public function getInfo($field=null)
        {
            if($this->termInfo!==null){
                $termInfo = $this->termInfo;
            }else{
                $termId=$this->termId;
                $termList=$this->getList();
                $termInfo=$termList[$termId];
                $this->termInfo=$termInfo;
            }
            if($field!==null){
                return $termInfo[$field];
            }
            return $termInfo;
        }

        public function getId()
        {
            return $this->termId;
        }
    }
    if(!isset($T)&&$T!==null)$T = new Term;